package com.wushi.lenovo.asia5b.app_ui.credit.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.mvp.helper.WCallBack;
import com.mvp.images.WImageLoader;
import com.mvp.ui.view.BaseTextWatcher;
import com.mvp.utils.InputMethodUtils;
import com.mvp.utils.ToastUtil;
import com.mvp.utils.Validator;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_config.CommonConstant;
import com.wushi.lenovo.asia5b.app_config.MemberBlanceRepository;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.credit.mvpview.TransferEmailActivityView;
import com.wushi.lenovo.asia5b.app_di.credit.presenter.TransferEmailActivityPresenter;
import com.wushi.lenovo.asia5b.app_model.credit.EmailModel;
import com.wushi.lenovo.asia5b.app_model.credit.MemberBlanceModel;
import com.wushi.lenovo.asia5b.app_ui.other.activity.MainActivity;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.trans.TransferDFragment;
import com.wushi.lenovo.asia5b.app_widget.ErrorTextView;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by zhangyuncai on 2019/1/16.
 * 邮箱转账
 */
public class TransferEmailActivity extends BaseMvpActivity< TransferEmailActivityView, TransferEmailActivityPresenter> implements TransferEmailActivityView {

    @BindView(R.id.civ_scandetail_head)
    CircleImageView civ_scandetail_head;
    @BindView(R.id.tv_trans_name)
    TextView tv_trans_name;
    @BindView(R.id.et_trans_email)
    TextView et_trans_email;
    @BindView(R.id.cons_trans_info)
    ConstraintLayout cons_trans_info;
    @BindView(R.id.tv_trans_name2)
    TextView tv_trans_name2;
    @BindView(R.id.et_trans_email2)
    EditText et_trans_email2;
    @BindView(R.id.iv_trans_icon)
    ImageView iv_trans_icon;
    @BindView(R.id.cons_trans_info2)
    ConstraintLayout cons_trans_info2;
    @BindView(R.id.et_trans_account)
    EditText et_trans_account;
    @BindView(R.id.tv_trans_amount)
    TextView tv_trans_amount;
    @BindView(R.id.bt_trans_proceed)
    Button bt_trans_proceed;
    @BindView(R.id.tv_amount_error)
    ErrorTextView tv_amount_error;
    @BindView(R.id.pb_trans)
    ProgressBar pb_trans;
    private EmailModel emailModel;//转账对象用户信息

    @Override
    protected int getLayoutId() {
        return R.layout.activity_transferemail;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        setEmailInfo();
    }

    private void setEmailInfo() {
        cons_trans_info.setVisibility(emailModel == null ? View.GONE : View.VISIBLE);
        cons_trans_info2.setVisibility(emailModel != null ? View.GONE : View.VISIBLE);
        if (emailModel != null) {
            tv_trans_name.setText(emailModel.getName());
            et_trans_email.setText(emailModel.getEmail());
            et_trans_email2.setText(emailModel.getEmail());

            tv_trans_name.setVisibility(TextUtils.isEmpty(emailModel.getName()) ? View.GONE : View.VISIBLE);
            WImageLoader.displayImage(this, emailModel.getAvatar_url(), civ_scandetail_head, R.drawable.icon_defaulthead);
        }
    }

    private void setIconRight(boolean right) {
        iv_trans_icon.setVisibility(View.VISIBLE);
        if (right) {
            iv_trans_icon.setImageResource(R.drawable.icon_trans_right);
            iv_trans_icon.setOnClickListener(v -> {
            });
        } else {
            iv_trans_icon.setImageResource(R.drawable.icon_trans_error);
            iv_trans_icon.setOnClickListener(v -> {
                et_trans_email2.setText("");
            });
        }
    }

    @Override
    protected void initData() {
        String email = getIntent().getStringExtra(CommonConstant.EMAIL);
        if (!TextUtils.isEmpty(email)) {
            et_trans_email2.setText(email);
            getPresenter().searchEmail(email);
        }
    }

    private void searchEmail(boolean closeKeyBoard) {
        String email = et_trans_email2.getText().toString().trim();
        if (!TextUtils.isEmpty(email)) {
            boolean b = Validator.isEmail(email);
            setIconRight(b);
            if (emailModel == null) {
                getPresenter().searchEmail(email);
            }
        } else {
            iv_trans_icon.setVisibility(View.GONE);
        }
        if (closeKeyBoard) {
            InputMethodUtils.closeKeyBoard(this);
        }
    }


    @Override
    protected void initEvent() {
        tv_amount_error.setBindEditText(et_trans_account);
        et_trans_account.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    searchEmail(false);
                }
            }
        });
        et_trans_email2.addTextChangedListener(new BaseTextWatcher() {
            @Override
            public void afterTextChanged(Editable editable) {
                String email = et_trans_email2.getText().toString().trim();
                if (!TextUtils.isEmpty(email)) {
                    boolean b = Validator.isEmail(email);
                    setIconRight(b);
                } else {
                    iv_trans_icon.setVisibility(View.GONE);
                }
            }
        });
    }

    private void showTransferDialog(String acountStr) {
        MemberBlanceModel memberBlanceModel = MemberBlanceRepository.getMemberBalance();
        if (memberBlanceModel == null) {
            ToastUtil.showToast(getString(R.string.paymentfailed));
            return;
        }
        TransferDFragment transferDFragment = (TransferDFragment) TransferDFragment.instantiate(this, TransferDFragment.class.getName());
        transferDFragment.setData(memberBlanceModel, emailModel, acountStr);
        transferDFragment.show(getSupportFragmentManager(), getClass().getName());
        transferDFragment.setCallBack(new WCallBack<ResultModel>() {
            @Override
            public void callback(ResultModel resultModel) {
                if (resultModel.isSuccess()) {
                    transferDFragment.setOnDismissListener(dialog -> {
                        startActivity(new Intent(mActivity, MainActivity.class));
                        finish();
                    });
                }
            }
        });

    }

    @OnClick({
            R.id.bt_trans_proceed,
            R.id.cons_trans_amount_vessel
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_trans_proceed:
                if (emailModel != null) {//已经存在正确的邮箱
                    String acountStr = et_trans_account.getText().toString();
                    if (!TextUtils.isEmpty(acountStr)) {
                        showTransferDialog(acountStr);
                    } else {
                        tv_amount_error.setVisibility(View.VISIBLE);
                    }
                } else {
                    searchEmail(true);
                }
                break;
            case R.id.cons_trans_amount_vessel: {
                searchEmail(true);
                break;
            }
            default:
                break;
        }

    }

    @Override
    public void searEmailResult(ResultModel<EmailModel> emailModelResultModel) {
        emailModel = emailModelResultModel.getData();
        setEmailInfo();
    }

    @Override
    public void loading(boolean b) {
        if (b) {
            pb_trans.setVisibility(View.VISIBLE);
            cons_trans_info2.setBackgroundColor(ContextCompat.getColor(this, R.color.common_bg_999));
        } else {
            pb_trans.setVisibility(View.GONE);
            cons_trans_info2.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        }
    }

}
